<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>好友留言板</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            font-size: 2.8rem;
            color: #4a69bd;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            color: #6a89cc;
            margin-bottom: 20px;
        }
        
        .message-form {
            background: white;
            padding: 25px;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }
        
        .form-header {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .avatar-upload {
            position: relative;
            margin-right: 15px;
        }
        
        .avatar-preview {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background: #f1f2f6;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            border: 2px dashed #a4b0be;
        }
        
        .avatar-preview img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .avatar-upload label {
            position: absolute;
            bottom: -5px;
            right: -5px;
            background: #4a69bd;
            color: white;
            width: 25px;
            height: 25px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 0.8rem;
        }
        
        .form-group {
            flex: 1;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 1rem;
            margin-bottom: 10px;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #4a69bd;
            box-shadow: 0 0 0 2px rgba(74, 105, 189, 0.2);
        }
        
        textarea {
            width: 100%;
            height: 120px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 1rem;
            resize: vertical;
            margin-bottom: 15px;
        }
        
        textarea:focus {
            outline: none;
            border-color: #4a69bd;
            box-shadow: 0 0 0 2px rgba(74, 105, 189, 0.2);
        }
        
        .submit-btn {
            background: #4a69bd;
            color: white;
            border: none;
            padding: 12px 25px;
            font-size: 1rem;
            font-weight: 600;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            display: block;
            margin-left: auto;
        }
        
        .submit-btn:hover {
            background: #1e3799;
            transform: translateY(-2px);
            box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
        }
        
        .messages-container {
            background: white;
            padding: 25px;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            font-size: 1.5rem;
            color: #4a69bd;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f1f2f6;
        }
        
        .message {
            display: flex;
            padding: 20px 0;
            border-bottom: 1px solid #f1f2f6;
        }
        
        .message:last-child {
            border-bottom: none;
        }
        
        .message-avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #f1f2f6;
            margin-right: 15px;
            overflow: hidden;
            flex-shrink: 0;
        }
        
        .message-avatar img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .message-content {
            flex: 1;
        }
        
        .message-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
        }
        
        .message-author {
            font-weight: 600;
            color: #1e3799;
        }
        
        .message-time {
            color: #a4b0be;
            font-size: 0.9rem;
        }
        
        .message-text {
            line-height: 1.6;
            margin-bottom: 15px;
        }
        
        .message-actions {
            display: flex;
            gap: 20px;
        }
        
        .action-btn {
            display: flex;
            align-items: center;
            gap: 5px;
            background: none;
            border: none;
            color: #a4b0be;
            cursor: pointer;
            font-size: 0.9rem;
            transition: all 0.2s ease;
        }
        
        .action-btn:hover {
            color: #4a69bd;
        }
        
        .action-btn.liked {
            color: #e55039;
        }
        
        .replies {
            margin-top: 15px;
            padding-left: 20px;
            border-left: 2px solid #f1f2f6;
        }
        
        .reply-form {
            display: flex;
            margin-top: 15px;
            gap: 10px;
        }
        
        .reply-input {
            flex: 1;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 20px;
            font-size: 0.9rem;
        }
        
        .reply-input:focus {
            outline: none;
            border-color: #4a69bd;
        }
        
        .reply-btn {
            background: #4a69bd;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 20px;
            cursor: pointer;
            font-size: 0.9rem;
        }
        
        .empty-message {
            text-align: center;
            padding: 40px 0;
            color: #a4b0be;
        }
        
        .empty-message i {
            font-size: 3rem;
            margin-bottom: 15px;
            color: #d1d8e0;
        }
        
        @media (max-width: 768px) {
            h1 {
                font-size: 2.2rem;
            }
            
            .message {
                flex-direction: column;
            }
            
            .message-avatar {
                margin-bottom: 10px;
            }
            
            .reply-form {
                flex-direction: column;
            }
            
            .reply-btn {
                align-self: flex-end;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>好友留言板</h1>
            <p class="subtitle">分享你的想法，与好友保持联系</p>
        </header>
        
        <div class="message-form">
            <div class="form-header">
                <div class="avatar-upload">
                    <div class="avatar-preview">
                        <i class="fas fa-user" style="font-size: 1.8rem; color: #a4b0be;"></i>
                    </div>
                    <label for="avatar-input">
                        <i class="fas fa-camera"></i>
                    </label>
                    <input type="file" id="avatar-input" accept="image/*" style="display: none;">
                </div>
                <div class="form-group">
                    <input type="text" id="author-name" placeholder="你的名字">
                </div>
            </div>
            <textarea id="message-text" placeholder="写下你想说的话..."></textarea>
            <button class="submit-btn" id="submit-message">发布留言</button>
        </div>
        
        <div class="messages-container">
            <h2 class="section-title">好友留言</h2>
            <div id="messages-list">
                <!-- 留言将在这里动态生成 -->
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const messagesList = document.getElementById('messages-list');
            const submitBtn = document.getElementById('submit-message');
            const authorInput = document.getElementById('author-name');
            const messageInput = document.getElementById('message-text');
            const avatarInput = document.getElementById('avatar-input');
            const avatarPreview = document.querySelector('.avatar-preview');
            
            // 留言数据
            let messages = [
                {
                    id: 1,
                    author: "李明",
                    avatar: "https://randomuser.me/api/portraits/men/32.jpg",
                    content: "大家周末有什么计划？我打算去爬山，有人一起吗？",
                    time: "2小时前",
                    likes: 8,
                    liked: false,
                    replies: [
                        {
                            id: 101,
                            author: "王芳",
                            content: "我想去！几点集合？",
                            time: "1小时前"
                        },
                        {
                            id: 102,
                            author: "张伟",
                            content: "这周末要加班，下次一定参加",
                            time: "45分钟前"
                        }
                    ]
                },
                {
                    id: 2,
                    author: "陈晓",
                    avatar: "https://randomuser.me/api/portraits/women/44.jpg",
                    content: "刚看完《流浪地球2》，特效太震撼了！强烈推荐给大家！",
                    time: "昨天",
                    likes: 15,
                    liked: true,
                    replies: [
                        {
                            id: 201,
                            author: "刘洋",
                            content: "我也看了，剧情和特效都超棒！",
                            time: "昨天"
                        }
                    ]
                },
                {
                    id: 3,
                    author: "赵婷",
                    avatar: "https://randomuser.me/api/portraits/women/68.jpg",
                    content: "分享一张今天拍的照片，夕阳下的城市太美了！",
                    time: "3天前",
                    likes: 24,
                    liked: false,
                    replies: []
                }
            ];
            
            // 头像上传处理
            avatarInput.addEventListener('change', function(e) {
                const file = e.target.files[0];
                if (file) {
                    const reader = new FileReader();
                    reader.onload = function(event) {
                        const img = document.createElement('img');
                        img.src = event.target.result;
                        avatarPreview.innerHTML = '';
                        avatarPreview.appendChild(img);
                    };
                    reader.readAsDataURL(file);
                }
            });
            
            // 渲染留言列表
            function renderMessages() {
                messagesList.innerHTML = '';
                
                if (messages.length === 0) {
                    messagesList.innerHTML = `
                        <div class="empty-message">
                            <i class="far fa-comment-alt"></i>
                            <p>还没有留言，快来发表第一条吧！</p>
                        </div>
                    `;
                    return;
                }
                
                messages.forEach(message => {
                    const messageElement = document.createElement('div');
                    messageElement.className = 'message';
                    messageElement.innerHTML = `
                        <div class="message-avatar">
                            <img src="${message.avatar}" alt="${message.author}">
                        </div>
                        <div class="message-content">
                            <div class="message-header">
                                <div class="message-author">${message.author}</div>
                                <div class="message-time">${message.time}</div>
                            </div>
                            <div class="message-text">${message.content}</div>
                            <div class="message-actions">
                                <button class="action-btn ${message.liked ? 'liked' : ''}" data-id="${message.id}">
                                    <i class="fas fa-heart"></i>
                                    <span>${message.likes}</span>
                                </button>
                                <button class="action-btn">
                                    <i class="fas fa-reply"></i>
                                    <span>回复</span>
                                </button>
                            </div>
                            ${renderReplies(message.replies)}
                            <div class="reply-form">
                                <input type="text" class="reply-input" placeholder="写下你的回复...">
                                <button class="reply-btn">发送</button>
                            </div>
                        </div>
                    `;
                    messagesList.appendChild(messageElement);
                });
                
                // 添加点赞事件监听
                document.querySelectorAll('.action-btn.like-btn').forEach(btn => {
                    btn.addEventListener('click', function() {
                        const messageId = parseInt(this.dataset.id);
                        toggleLike(messageId);
                    });
                });
            }
            
            // 渲染回复
            function renderReplies(replies) {
                if (replies.length === 0) return '';
                
                let repliesHtml = '<div class="replies">';
                replies.forEach(reply => {
                    repliesHtml += `
                        <div class="message">
                            <div class="message-avatar">
                                <i class="fas fa-user"></i>
                            </div>
                            <div class="message-content">
                                <div class="message-header">
                                    <div class="message-author">${reply.author}</div>
                                    <div class="message-time">${reply.time}</div>
                                </div>
                                <div class="message-text">${reply.content}</div>
                            </div>
                        </div>
                    `;
                });
                repliesHtml += '</div>';
                return repliesHtml;
            }
            
            // 点赞功能
            function toggleLike(messageId) {
                const message = messages.find(m => m.id === messageId);
                if (message) {
                    message.liked = !message.liked;
                    message.likes += message.liked ? 1 : -1;
                    renderMessages();
                }
            }
            
            // 发布新留言
            submitBtn.addEventListener('click', function() {
                const author = authorInput.value.trim();
                const content = messageInput.value.trim();
                
                if (!author || !content) {
                    alert('请填写名字和留言内容');
                    return;
                }
                
                // 获取头像
                let avatar = '';
                const avatarImg = avatarPreview.querySelector('img');
                if (avatarImg) {
                    avatar = avatarImg.src;
                }
                
                // 创建新留言
                const newMessage = {
                    id: Date.now(),
                    author: author,
                    avatar: avatar || '',
                    content: content,
                    time: '刚刚',
                    likes: 0,
                    liked: false,
                    replies: []
                };
                
                messages.unshift(newMessage);
                renderMessages();
                
                // 重置表单
                authorInput.value = '';
                messageInput.value = '';
                avatarPreview.innerHTML = '<i class="fas fa-user" style="font-size: 1.8rem; color: #a4b0be;"></i>';
            });
            
            // 初始化渲染
            renderMessages();
        });
    </script>
</body>
</html>